/******************************************************************************
 *
 * Copyright (C) 2009 - 2014 Xilinx, Inc.  All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * Use of the Software is limited solely to applications:
 * (a) running on a Xilinx device, or
 * (b) that interact with a Xilinx device through a bus or interconnect.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Except as contained in this notice, the name of the Xilinx shall not be used
 * in advertising or otherwise to promote the sale, use or other dealings in
 * this Software without prior written authorization from Xilinx.
 *
 ******************************************************************************/

/*
 * helloworld.c: simple test application
 *
 * This application configures UART 16550 to baud rate 9600.
 * PS7 UART (Zynq) is not initialized by this application, since
 * bootrom/bsp configures it to baud rate 115200
 *
 * ------------------------------------------------
 * | UART TYPE   BAUD RATE                        |
 * ------------------------------------------------
 *   uartns550   9600
 *   uartlite    Configurable only in HW design
 *   ps7_uart    115200 (configured by bootrom/bsp)
 */

#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "sleep.h"
#include "xil_exception.h"
#include "xdebug.h"
#include "xuartlite.h"
#include "xuartlite_l.h"
#include "xintc.h"
#include "shell.h"
#include "xgpio.h"
#include "io_board_func.h"

#define UART_DEVICE_ID XPAR_UARTLITE_0_DEVICE_ID   
#define UART_INTR_ID XPAR_INTC_0_UARTLITE_0_VEC_ID
#define INTC_ID XPAR_INTC_0_DEVICE_ID 

#define RX_NOEMPTY XUL_SR_RX_FIFO_VALID_DATA 



static XIntc Intc;
static XUartLite Uart;

void uart_handler(void *CallbackRef);
void shell_write(const char data);
SHELL_TypeDef shell;

XGpio   gpio;

int main()
{
    XUartLite_Initialize(&Uart, UART_DEVICE_ID);
    shell.write = shell_write;
    shellInit(&shell);
    XIntc_Initialize(&Intc, INTC_ID);
    XIntc_Connect(&Intc, UART_INTR_ID, (XInterruptHandler)uart_handler, &Uart);
    XUartLite_EnableInterrupt(&Uart);
    XIntc_Start(&Intc, XIN_REAL_MODE);

    XIntc_Enable(&Intc, UART_INTR_ID);

    Xil_ExceptionInit();
    Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, (Xil_ExceptionHandler)XIntc_InterruptHandler, &Intc);
    Xil_ExceptionEnable();


    XGpio_Initialize(&gpio, LED_ID);
    XGpio_SetDataDirection(&gpio, LED_CHANNEL, 0);

    int i = 0;
    while (1)
    {
        // sleep(1);
        // if(i == 3) i = 0;
        // else i = i + 1;
        // XGpio_DiscreteWrite(&gpio, LED_CHANNEL, 0x01 << i);
        // print("hello\n\r");
    }
    return 0;
}

void uart_handler(void *CallbackRef)
{
    u8 Read_data;
    u32 isr_status;
    XUartLite *InstancePtr = (XUartLite *)CallbackRef;
    isr_status = XUartLite_ReadReg(InstancePtr->RegBaseAddress, XUL_STATUS_REG_OFFSET);
    if (isr_status & RX_NOEMPTY)
    {
        Read_data = XUartLite_ReadReg(InstancePtr->RegBaseAddress, XUL_RX_FIFO_OFFSET);
        shellHandler(&shell, Read_data);
    }
}

void shell_write(const char data)
{
    // XUartLite_WriteReg(Uart.RegBaseAddress, XUL_TX_FIFO_OFFSET, data);
    outbyte(data);
}
